
# 特征工程类
class FeatureEngineer:
    def __init__(self):
        self.tfidf_vectorizer = None
        self.lexical_features = ['avg_word_length', 'word_count', 'unique_word_ratio']
        
    def create_tfidf_features(self, texts, max_features=5000, ngram_range=(1, 2)):
        """创建TF-IDF特征"""
        self.tfidf_vectorizer = TfidfVectorizer(
            max_features=max_features,
            ngram_range=ngram_range,
            min_df=2,
            max_df=0.8,
            sublinear_tf=True  # 使用亚线性TF缩放
        )
        tfidf_features = self.tfidf_vectorizer.fit_transform(texts)
        return tfidf_features
    
    def extract_lexical_features(self, texts):
        """提取词汇特征"""
        features = []
        for text in texts:
            words = text.split()
            if len(words) == 0:
                features.append([0, 0, 0])
                continue
                
            avg_word_length = np.mean([len(word) for word in words])
            word_count = len(words)
            unique_word_ratio = len(set(words)) / len(words) if len(words) > 0 else 0
            
            features.append([avg_word_length, word_count, unique_word_ratio])
        
        return np.array(features)
    
    def create_all_features(self, texts):
        """创建所有特征"""
        # TF-IDF特征
        tfidf_features = self.create_tfidf_features(texts)
        
        # 词汇特征
        lexical_features = self.extract_lexical_features(texts)
        
        # 组合特征
        from scipy.sparse import hstack
        all_features = hstack([tfidf_features, lexical_features])
        
        return all_features